package com.bkgy.modular.qms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bkgy.modular.qms.dao.*;
import com.bkgy.modular.qms.entity.*;
import com.bkgy.modular.tbdsjdatacollect.dao.TbDsjDataCollectDao;
import com.bkgy.modular.tbdsjdatacollect.entity.TbDsjDataCollect;
import com.bkgy.modular.tbdsjruleedit.dao.TbDsjRuleEditDao;
import com.bkgy.modular.tbdsjruleedit.entity.TbDsjRuleEdit;
import com.bkgy.sys.common.entity.ResultData;
import com.bkgy.util.CalUtils;
import com.bkgy.util.PojoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 冷咋轧PDI热轧QMSBlob，热轧PDI PDO一块的
 * 接收产销系统原料电文（先用宝信的头部焊接信号代替）
 * QMS读取冷轧的pdi数据（根据出口卷号读取宝信DB2）
 * 读取qms的oracle数据库中长度归档数据(逗号分隔字符串解析)（读取变量采集表）
 * 根据3.5计算相应特征值（规则编辑对应表）
 */
@Service
@Slf4j
public class QmsServiceImpl {

    @Resource
    private QmsColdPdiDao621 qmsColdPdiDao621;
    @Resource
    private QmsColdPdiDao622 qmsColdPdiDao622;
    @Resource
    private QmsColdPdiDao642 qmsColdPdiDao642;
    @Resource
    private QmsColdPdiDao651652 qmsColdPdiDao651652;

    @Resource
    private TbDsjDataCollectDao tbDsjDataCollectDao;

    @Resource
    private TbDsjRuleEditDao tbDsjRuleEditDao;

    @Resource
    private QmsCurveDataDao qmsCurveDataDao;

    @Resource
    private CalUtils calUtils;

    @Resource
    private QmsColdPdiMongoDao qmsColdPdiMongoDao;

    @Resource
    private QmsCurveDataMongoDao qmsCurveDataMongoDao;

    @Resource
    private PojoUtils pojoUtils;

    @Resource
    private Hsm1PdiDao hsm1PdiDao;

    @Resource
    private Hsm1PdoDao hsm1PdoDao;

    @Resource
    private Hsm2PdiDao hsm2PdiDao;

    @Resource
    private Hsm2PdoDao hsm2PdoDao;

    @Resource
    private Q651TPdoDataMapper q651TPdoDataMapper;

    @Resource
    private Q652TPdoDataMapper q652TPdoDataMapper;

    @Resource
    private TbDsjColdNlHotMapper tbDsjColdNlHotMapper;

    @Resource
    private MongoTemplate mongoTemplate;

    @Resource
    private Q651TPdiDataMapper q651TPdiDataMapper;

    @Resource
    private Q652TPdiDataMapper q652TPdiDataMapper;

    public void readColdPdi(String volumeNo, String line) {
        try {
            //1.保存冷轧PDI数据
            QueryWrapper<QmsColdPdi621> wrapper621 = new QueryWrapper<>();
            wrapper621.eq("EXTID", volumeNo);

            QueryWrapper<QmsColdPdi622> wrapper622 = new QueryWrapper<>();
            wrapper622.eq("EXTID", volumeNo);

            QueryWrapper<QmsColdPdi642> wrapper642 = new QueryWrapper<>();
            wrapper642.eq("EXTID", volumeNo);
            //冷轧数据pdi数据获取
            QmsColdPdi621 qmsColdPdi621 = null;
            QmsColdPdi622 qmsColdPdi622 = null;
            QmsColdPdi642 qmsColdPdi642 = null;
            //冷轧pdi MongoDB保存对象
            QmsColdPdiMongoDB qmsColdPdiMongoDB = new QmsColdPdiMongoDB();
            //设置卷号
            qmsColdPdiMongoDB.setVolume(volumeNo);
            //字段赋值
            Map<String, Object> mapResult = new HashMap<>();
            if (line.equals("621")) {
                qmsColdPdi621 = qmsColdPdiDao621.selectOne(wrapper621);
                if (!StringUtils.isEmpty(qmsColdPdi621)) {
                    Map<String, Object> map = pojoUtils.beanToMap(qmsColdPdi621);
                    //查询数据采集表里，需要保存的字段
                    QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("PRODUCTION_LINE", "C621_PDI");
                    queryWrapper.eq("out_data_source", 5);
                    List<TbDsjDataCollect> alias621 = tbDsjDataCollectDao.selectList(queryWrapper);
                    if (!StringUtils.isEmpty(alias621) && !alias621.isEmpty()) {
//                        List<String> aliasList = alias621.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());
//                        map.keySet().forEach(t -> {
//                            TbDsjDataCollect tbDsjDataCollect = alias621.stream().filter(w -> w.getL1_variable_name().equalsIgnoreCase(t)).findFirst().orElse(null);
//                            if(!StringUtils.isEmpty(tbDsjDataCollect)){
//                                mapResult.put(tbDsjDataCollect.getL1_variable_alias(), map.get(t));
//                            }
//                        });
                        alias621.forEach(t->{
                            if(!StringUtils.isEmpty(map.get(t.getL1_variable_name().toLowerCase()))){
                                mapResult.put(t.getL1_variable_alias(),map.get(t.getL1_variable_name().toLowerCase()));
                            }
                        });


                    }
                    qmsColdPdiMongoDB.setMap(mapResult);
                    qmsColdPdiMongoDao.save(qmsColdPdiMongoDB);
                    log.info("冷轧pdi621数据存储成功");
                }
            }
//            QmsColdPdi qmsColdPdi = null;
            if (line.equals("622")) {
                qmsColdPdi622 = qmsColdPdiDao622.selectOne(wrapper622);
                if (!StringUtils.isEmpty(qmsColdPdi622)) {
                    Map<String, Object> map = pojoUtils.beanToMap(qmsColdPdi622);
                    //查询数据采集表里，需要保存的字段
                    QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("PRODUCTION_LINE", "C622_PDI");
                    queryWrapper.eq("out_data_source", 5);
                    List<TbDsjDataCollect> alias622 = tbDsjDataCollectDao.selectList(queryWrapper);
                    if (!StringUtils.isEmpty(alias622) && !alias622.isEmpty()) {
//                        List<String> aliasList = alias622.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());
//                        List<String> bxNameList = alias622.stream().map(TbDsjDataCollect::getL1_variable_name).collect(Collectors.toList());

//                        map.keySet().forEach(t -> {
//                            TbDsjDataCollect tbDsjDataCollect = alias622.stream().filter(w -> w.getL1_variable_name().equalsIgnoreCase(t)).findFirst().orElse(null);
//                            if(!StringUtils.isEmpty(tbDsjDataCollect)){
//                                mapResult.put(tbDsjDataCollect.getL1_variable_alias(), map.get(t));
//                            }
//                        });
                        alias622.forEach(t->{
                            if(!StringUtils.isEmpty(map.get(t.getL1_variable_name().toLowerCase()))){
                                mapResult.put(t.getL1_variable_alias(),map.get(t.getL1_variable_name().toLowerCase()));
                            }
                        });

                    }
                    qmsColdPdiMongoDB.setMap(mapResult);
                    qmsColdPdiMongoDao.save(qmsColdPdiMongoDB);
                    log.info("冷轧pdi622数据存储成功");
                }
            }
            if (line.equals("642")) {
                qmsColdPdi642 = qmsColdPdiDao642.selectOne(wrapper642);
                if (!StringUtils.isEmpty(qmsColdPdi642)) {
                    Map<String, Object> map = pojoUtils.beanToMap(qmsColdPdi642);
                    //查询数据采集表里，需要保存的字段
                    QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("PRODUCTION_LINE", "C642_PDI");
                    queryWrapper.eq("out_data_source", 5);
                    List<TbDsjDataCollect> alias642 = tbDsjDataCollectDao.selectList(queryWrapper);
                    if (!StringUtils.isEmpty(alias642) && !alias642.isEmpty()) {
//                        List<String> aliasList = alias642.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());
//                        map.keySet().forEach(t -> {
//                            TbDsjDataCollect tbDsjDataCollect = alias642.stream().filter(w -> w.getL1_variable_name().equalsIgnoreCase(t)).findFirst().orElse(null);
//                            if(!StringUtils.isEmpty(tbDsjDataCollect)){
//                                mapResult.put(tbDsjDataCollect.getL1_variable_alias(), map.get(t));
//                            }
//                        });
                        alias642.forEach(t->{
                            if(!StringUtils.isEmpty(map.get(t.getL1_variable_name().toLowerCase()))){
                                mapResult.put(t.getL1_variable_alias(),map.get(t.getL1_variable_name().toLowerCase()));
                            }
                        });
                    }
                    qmsColdPdiMongoDB.setMap(mapResult);
                    qmsColdPdiMongoDao.save(qmsColdPdiMongoDB);
                    log.info("冷轧pdi642数据存储成功");
                }
            }
        } catch (Exception e) {
            log.info("保存冷轧Pdi信息失败:" + e);
            e.printStackTrace();
        }
    }

    public  ResultData hotRollingBoldData(String volumeNo,String width) {
        //2.准备处理宝信qms数据Bolb
        //1.新建保存mongodb对象
        QmsCurveDataMongo qmsCurveDataMongo = new QmsCurveDataMongo();
        try {

            //根据数据采集表里，查出所需的qms宝信名称和内部别名
            QueryWrapper<TbDsjDataCollect> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("out_data_source", 2);
            queryWrapper.eq("variable_property", 2);
            List<TbDsjDataCollect> alias = tbDsjDataCollectDao.selectList(queryWrapper);
            Map<String, String> nameMap = alias.stream().collect(Collectors.toMap(t -> t.getL1_variable_name(), t -> t.getL1_variable_alias()));

            //            List<TbDsjDataCollect> alias = tbDsjDataCollectDao.findAlias(null, 2);
            //单独获取别名，用于后面查询特征值
            List<String> aliasOracle = alias.stream().map(TbDsjDataCollect::getL1_variable_alias).collect(Collectors.toList());
            //单独获取原始名
            List<String> nameOracle = alias.stream().map(TbDsjDataCollect::getL1_variable_name).collect(Collectors.toList());

            nameOracle = nameOracle.stream().filter(t -> !t.isEmpty()).collect(Collectors.toList());
            //根据原始名和卷号,查询qmsOracle里面的Blob数据实体类
            List<QmsCurveData> qmsCurveDataList = null;
            if (!StringUtils.isEmpty(volumeNo) && !StringUtils.isEmpty(nameOracle)) {
                QueryWrapper<QmsCurveData> wrapperOracle = new QueryWrapper<>();
                wrapperOracle.likeRight("MAT_NO", volumeNo.substring(0,volumeNo.length()-1));
                wrapperOracle.in("Name", nameOracle);
                qmsCurveDataList = qmsCurveDataDao.selectList(wrapperOracle);

            }
            //如果直接查询热轧卷号，查不到就到中间常化表里查
            if (qmsCurveDataList.isEmpty()) {
                String no651 = qmsColdPdiDao651652.findNo651(volumeNo);
                if(!StringUtils.isEmpty(no651)){
                    volumeNo = no651;
                }
                if (!StringUtils.isEmpty(volumeNo) && !StringUtils.isEmpty(nameOracle)) {
                    QueryWrapper<QmsCurveData> wrapperOracle = new QueryWrapper<>();
                    wrapperOracle.likeRight("MAT_NO", volumeNo.substring(0,volumeNo.length()-1));
                    wrapperOracle.in("Name", nameOracle);
                    qmsCurveDataList = qmsCurveDataDao.selectList(wrapperOracle);
                }
            }
            if (qmsCurveDataList.isEmpty()) {
                String no652 = qmsColdPdiDao651652.findNo652(volumeNo);
                if(!StringUtils.isEmpty(no652)){
                    volumeNo = no652;
                }
                if (!StringUtils.isEmpty(volumeNo) && !StringUtils.isEmpty(nameOracle)) {
                    QueryWrapper<QmsCurveData> wrapperOracle = new QueryWrapper<>();
                    wrapperOracle.likeRight("MAT_NO", volumeNo.substring(0,volumeNo.length()-1));
                    wrapperOracle.in("Name", nameOracle);
                    qmsCurveDataList = qmsCurveDataDao.selectList(wrapperOracle);
                }
            }

            //将blob数据处理成Double
            Map<String, List<Double>> originBolg = new HashMap<>();
            if (!StringUtils.isEmpty(qmsCurveDataList)) {
                qmsCurveDataList.forEach(t -> {
                    List<Double> blobData1 = calUtils.analysisBlobData(t.getData());
                    String mat_no = t.getMat_no();
                    String begin = mat_no.substring(0, 1);
                    String end = mat_no.substring(mat_no.length() - 1);
                    char c = end.toCharArray()[0];
                    //卷号是H、M开头
                    if(begin.equals("H")||begin.equals("M")){
                        //结尾为字母头尾调换,数字不调换
                        if(Character.isDigit(c)){
                            originBolg.put(nameMap.get(t.getName()),blobData1);
                        }else {
                            Collections.reverse(blobData1);
                            originBolg.put(nameMap.get(t.getName()),blobData1);
                        }
                    }else {
                         // Collections.reverse(blobData1);
                        originBolg.put(nameMap.get(t.getName()),blobData1);
                    }
                });
            }


            //准备处理特征值
            List<QmsCurveData> qmsCurveData1 = new ArrayList<>();
            if (!StringUtils.isEmpty(qmsCurveDataList)) {
                qmsCurveData1.addAll(qmsCurveDataList);
            }

            //根据规则表查询出需要计算特征值的变量实体
            QueryWrapper<TbDsjRuleEdit> wrapperRule = new QueryWrapper<>();
            wrapperRule.in("dic_variable_name_calc", aliasOracle);
            List<TbDsjRuleEdit> tbDsjRuleEdits = tbDsjRuleEditDao.selectList(wrapperRule);

            //存放特征值
            HashMap<String, Double> trailValues = new HashMap<>();

            if (!tbDsjRuleEdits.isEmpty()) {
                tbDsjRuleEdits.forEach(t -> {
                    //按照变量别名查找过滤需要计算对应特征值的Bolb原数据
                    QmsCurveData qmsCurveData = null;
                    TbDsjDataCollect tbDsjDataCollect = null;
                    List<TbDsjDataCollect> collectList = alias.stream().filter(k -> k.getL1_variable_alias().equals(t.getDic_variable_name_calc())).collect(Collectors.toList());
                    TbDsjDataCollect finalTbDsjDataCollect = tbDsjDataCollect;
                    if(collectList.size() > 0){
                        for (QmsCurveData item : qmsCurveData1) {
                            for (TbDsjDataCollect dsjDataCollect : collectList) {
                                if (item.getName().equals(dsjDataCollect.getL1_variable_name())) {
                                    qmsCurveData = item;
                                }
                            }
                        }
                    }

                    //根据不同规则分别计算
                    if (!StringUtils.isEmpty(qmsCurveData) && !StringUtils.isEmpty(t.getRule_editor()) && t.getRule_editor().equals("avg")) {
                        byte[] blob = qmsCurveData.getData();
                        if (!StringUtils.isEmpty(blob)) {
                            List<Double> blobData = calUtils.analysisBlobData(blob);
                            Double avg = calUtils.calAvg(blobData);
                            trailValues.put(t.getCalc_result_variable(), avg);
                        }
                    }
                    if (!StringUtils.isEmpty(qmsCurveData) && !StringUtils.isEmpty(t.getRule_editor()) && t.getRule_editor().equals("max")) {
                        byte[] blob = qmsCurveData.getData();
                        if (!StringUtils.isEmpty(blob)) {
                            List<Double> blobData = calUtils.analysisBlobData(blob);
                            Double max = calUtils.calMax(blobData);
                            trailValues.put(t.getCalc_result_variable(), max);
                        }
                    }
                    if (!StringUtils.isEmpty(qmsCurveData) && !StringUtils.isEmpty(t.getRule_editor()) && t.getRule_editor().equals("min")) {
                        byte[] blob = qmsCurveData.getData();
                        if (!StringUtils.isEmpty(blob)) {
                            List<Double> blobData = calUtils.analysisBlobData(blob);
                            Double min = calUtils.calMin(blobData);
                            trailValues.put(t.getCalc_result_variable(), min);
                        }
                    }
                });
            }

            if (!originBolg.isEmpty() || !trailValues.isEmpty()) {
                qmsCurveDataMongo.setOriginBolg(originBolg);
                qmsCurveDataMongo.setTrailValues(trailValues);
                log.info("QMS的Blob数据处理完毕");
            }else {
                log.info("QMS的Blob数据为空暂不保存");
            }

            Map<String,List<Double>> map = new HashMap<>();
            for (TbDsjDataCollect item : alias) {
                if ((volumeNo.startsWith("M") && item.getL1_variable_name().equals("HSM2.FDW.MFG_WIDTH_DEV")) || (volumeNo.startsWith("H") && item.getL1_variable_name().equals("HSM1.FDW.MFG_WIDTH_DEV"))) {
                    String l1_variable_name = item.getL1_variable_name();
                    LambdaQueryWrapper<QmsCurveData> queryWrapper1 = new LambdaQueryWrapper<>();
                    String finalVolumeNo = volumeNo;
                    System.out.println(finalVolumeNo.substring(0,9));
                    queryWrapper1.and(item2 -> item2.eq(QmsCurveData::getName,l1_variable_name).like(QmsCurveData::getMat_no, finalVolumeNo.substring(0,9)));
                    QmsCurveData qmsCurveData = qmsCurveDataDao.selectOne(queryWrapper1);
                    if (qmsCurveData != null) {
                        byte[] data = qmsCurveData.getData();
                        List<Double> blobData = calUtils.analysisBlobData(data);
                        List<Double> newData = new ArrayList<>();
                        for (Double datum : blobData) {
                            if (width != null) {
                                newData.add(Double.parseDouble(datum + Double.parseDouble(width) + ""));
                            }
                        }
                        Map<String, List<Double>> originBolg1 = qmsCurveDataMongo.getOriginBolg();
                        originBolg1.put("hsm_fdw_width_cold",newData);
                        qmsCurveDataMongo.setOriginBolg(originBolg1);
                    }
                }
            }

        } catch (Exception e) {
            log.info("保存qms信息失败:" + e);
            e.printStackTrace();
        }


        qmsCurveDataMongo.setVolumeNo(volumeNo);
        Map<String, Object> hotPdi = findHotPdi(volumeNo);
        Map<String, Object> hotPdo = findHotPdo(volumeNo);
        qmsCurveDataMongo.setPdi(hotPdi);
        qmsCurveDataMongo.setPdo(hotPdo);
        qmsCurveDataMongo.setCreatetime(new Date());
        QmsCurveDataMongo mongoData = qmsCurveDataMongoDao.save(qmsCurveDataMongo);
        if (mongoData != null) {
            return ResultData.ResultDataFaultSelf("保存成功",1);
        } else {
            return ResultData.ResultDataFaultSelf("保存失败",0);
        }
    }

    private Map<String, Object> findHotPdi(String volumeNo){
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<TbDsjDataCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("variable_property",1);
        wrapper.eq("out_data_source",2);
        wrapper.eq("device_code","PDI");
        List<TbDsjDataCollect> collectList = tbDsjDataCollectDao.selectList(wrapper);

        QueryWrapper<Hsm1Pdi> wrapperH1Pdi = new QueryWrapper<>();
        wrapperH1Pdi.likeRight("l2_coil_id",volumeNo.substring(0,volumeNo.length()-1));
        List<Hsm1Pdi> hsm1PdiList = hsm1PdiDao.selectList(wrapperH1Pdi);

        if(!hsm1PdiList.isEmpty()){
            hsm1PdiList.forEach(t ->{
                Map<String, Object> bean = pojoUtils.beanToMap(t);
                for (TbDsjDataCollect collect : collectList) {
                    if(bean.get(collect.getL1_variable_name().toLowerCase())!=null&&!StringUtils.isEmpty(bean.get(collect.getL1_variable_name().toLowerCase()))){
                        map.put(collect.getL1_variable_alias(),bean.get(collect.getL1_variable_name().toLowerCase()));
                    }
                }
            });
        }

        QueryWrapper<Hsm2Pdi> wrapperH2Pdi = new QueryWrapper<>();
        wrapperH2Pdi.likeRight("c_coilid",volumeNo.substring(0,volumeNo.length()-1));
        List<Hsm2Pdi> hsm2PdiList = hsm2PdiDao.selectList(wrapperH2Pdi);

        if(map.isEmpty()&&!hsm2PdiList.isEmpty()){
            hsm2PdiList.forEach(t ->{
                Map<String, Object> bean = pojoUtils.beanToMap(t);
                for (TbDsjDataCollect collect : collectList) {
                    if(bean.get(collect.getL1_variable_name().toLowerCase())!=null&&!StringUtils.isEmpty(bean.get(collect.getL1_variable_name().toLowerCase()))){
                        map.put(collect.getL1_variable_alias(),bean.get(collect.getL1_variable_name().toLowerCase()));
                    }
                }
            });
        }
        return map;
    }

    private Map<String, Object> findHotPdo(String volumeNo){
        Map<String, Object> map = new HashMap<>();
        QueryWrapper<TbDsjDataCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("variable_property",1);
        wrapper.eq("out_data_source",2);
        wrapper.eq("device_code","PDO");
        List<TbDsjDataCollect> collectList = tbDsjDataCollectDao.selectList(wrapper);

        QueryWrapper<Hsm1Pdo> wrapperH1Pdo = new QueryWrapper<>();
        wrapperH1Pdo.likeRight("coilid",volumeNo.substring(0,volumeNo.length()-1));
        List<Hsm1Pdo> hsm1PdoList = hsm1PdoDao.selectList(wrapperH1Pdo);

        if(!hsm1PdoList.isEmpty()){
            hsm1PdoList.forEach(t ->{
                Map<String, Object> bean = pojoUtils.beanToMap(t);
                for (TbDsjDataCollect collect : collectList) {
                    if(bean.get(collect.getL1_variable_name().toLowerCase())!=null&&!StringUtils.isEmpty(bean.get(collect.getL1_variable_name().toLowerCase()))){
                        map.put(collect.getL1_variable_alias(),bean.get(collect.getL1_variable_name().toLowerCase()));
                    }
                }
            });
        }

        QueryWrapper<Hsm2Pdo> wrapperH2Pdo = new QueryWrapper<>();
        wrapperH2Pdo.likeRight("act_coil_id",volumeNo.substring(0,volumeNo.length()-1));
        List<Hsm2Pdo> hsm2PdoList = hsm2PdoDao.selectList(wrapperH2Pdo);

        if(map.isEmpty()&&!hsm2PdoList.isEmpty()){
            hsm2PdoList.forEach(t ->{
                Map<String, Object> bean = pojoUtils.beanToMap(t);
                for (TbDsjDataCollect collect : collectList) {
                    if(bean.get(collect.getL1_variable_name().toLowerCase())!=null&&!StringUtils.isEmpty(bean.get(collect.getL1_variable_name().toLowerCase()))){
                        map.put(collect.getL1_variable_alias(),bean.get(collect.getL1_variable_name().toLowerCase()));
                    }
                }
            });
        }
        return map;
    }


    public ResultData getBXFindData(String volumeNo,String width) {
        LambdaQueryWrapper<Q651TPdoData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Q651TPdoData::getExtid,volumeNo);
        Q651TPdoData q651TPdoData = q651TPdoDataMapper.selectOne(queryWrapper);
        if (q651TPdoData != null) {
            // 使用pdo的入口卷号查询pdi的数据
            LambdaQueryWrapper<Q651TPdiData> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Q651TPdiData::getEntid,q651TPdoData.getEntid());
            Q651TPdiData q651TPdiData = q651TPdiDataMapper.selectOne(queryWrapper1);
            Query query = new Query(Criteria.where("volumeNo").is(q651TPdoData.getEntid()));
            List<QmsCurveDataMongo> list = mongoTemplate.find(query, QmsCurveDataMongo.class);
            if (list.size() == 0) {
                this.hotRollingBoldData(volumeNo,width);
            }
            TbDsjColdNlHot tbDsjColdNlHot = new TbDsjColdNlHot();
            tbDsjColdNlHot.setHot_coil_id(q651TPdoData.getEntid());
            tbDsjColdNlHot.setCold_entry_id(q651TPdoData.getExtid());
            tbDsjColdNlHot.setNormalization_code("Q651");
            tbDsjColdNlHot.setNormalization_name("二硅钢常化酸洗线");
            tbDsjColdNlHot.setNormalization_extid(q651TPdoData.getExtid());
            tbDsjColdNlHot.setNormalization_entid(q651TPdoData.getEntid());
            tbDsjColdNlHot.setN_prod_time(q651TPdoData.getTime_end());
            tbDsjColdNlHot.setAlloy_code(q651TPdoData.getAlloy_code());
            tbDsjColdNlHot.setWeight_act(q651TPdoData.getWeight_act());
            tbDsjColdNlHot.setExit_width(q651TPdoData.getExit_width());
            tbDsjColdNlHot.setExit_thickness(q651TPdoData.getExit_thickness()/1000);
            tbDsjColdNlHot.setExit_length(q651TPdoData.getExit_length());
            tbDsjColdNlHot.setEx_diam_in(q651TPdoData.getExit_innerdia());
            tbDsjColdNlHot.setEx_diam_out(q651TPdoData.getExit_outterdia());
            tbDsjColdNlHot.setProd_start(q651TPdoData.getTime_start());
            if (q651TPdiData != null) {
                tbDsjColdNlHot.setEn_thk(q651TPdiData.getEntry_thickness()/1000);
                tbDsjColdNlHot.setEn_wid(q651TPdiData.getEntry_width());
                tbDsjColdNlHot.setEn_len(q651TPdiData.getEntry_length());
                tbDsjColdNlHot.setEn_wt(q651TPdiData.getEntry_weight());
                tbDsjColdNlHot.setEn_diam_in(q651TPdiData.getEntry_indiam());
                tbDsjColdNlHot.setEn_diam_out(q651TPdiData.getEntry_outdiam());
                tbDsjColdNlHot.setTrim_wid(q651TPdiData.getTrim_width());
            }
            LambdaQueryWrapper<TbDsjColdNlHot> qwr = new LambdaQueryWrapper<>();
            qwr.eq(TbDsjColdNlHot::getCold_entry_id,q651TPdoData.getExtid());
            TbDsjColdNlHot dsjColdNlHot = tbDsjColdNlHotMapper.selectOne(qwr);
            if (dsjColdNlHot != null) {
                return ResultData.ResultDataFaultSelf("该卷号已存在,请查看参数是否正确!",-1);
            }
            int saveState = tbDsjColdNlHotMapper.insert(tbDsjColdNlHot);
            return ResultData.ResultDataFaultSelf("操作成功",saveState);
        }
        LambdaQueryWrapper<Q652TPdoData> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Q652TPdoData::getExtid,volumeNo);
        Q652TPdoData q652TPdoData = q652TPdoDataMapper.selectOne(queryWrapper1);
        if (q652TPdoData != null) {
            // 使用pdo的入口卷号查询pdi的数据
            LambdaQueryWrapper<Q652TPdiData> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(Q652TPdiData::getEntid,q652TPdoData.getEntid());
            Q652TPdiData q652TPdiData = q652TPdiDataMapper.selectOne(queryWrapper2);


            Query query2 = new Query(Criteria.where("volumeNo").is(q652TPdoData.getEntid()));
            List<QmsCurveDataMongo> list2 = mongoTemplate.find(query2, QmsCurveDataMongo.class);
            if (list2.size() == 0) {
                this.hotRollingBoldData(volumeNo,width);
            }
            TbDsjColdNlHot tbDsjColdNlHot = new TbDsjColdNlHot();
            tbDsjColdNlHot.setHot_coil_id(q652TPdoData.getEntid());
            tbDsjColdNlHot.setCold_entry_id(q652TPdoData.getExtid());
            tbDsjColdNlHot.setNormalization_code("Q652");
            tbDsjColdNlHot.setNormalization_name("二硅钢常化线");
            tbDsjColdNlHot.setNormalization_extid(q652TPdoData.getExtid());
            tbDsjColdNlHot.setNormalization_entid(q652TPdoData.getEntid());
            tbDsjColdNlHot.setN_prod_time(q652TPdoData.getProd_end_utc());
            tbDsjColdNlHot.setAlloy_code(q652TPdoData.getAlloy_code());
            tbDsjColdNlHot.setWeight_act(q652TPdoData.getWeight());
            tbDsjColdNlHot.setExit_width(q652TPdoData.getWidth());
            tbDsjColdNlHot.setExit_thickness(q652TPdoData.getThickness());
            tbDsjColdNlHot.setExit_length(q652TPdoData.getLength());
            tbDsjColdNlHot.setEx_diam_in(q652TPdoData.getDiam_inside());
            tbDsjColdNlHot.setEx_diam_out(q652TPdoData.getDiam_outside());
            tbDsjColdNlHot.setProd_start(q652TPdoData.getProd_start());
            if (q652TPdiData != null) {
                tbDsjColdNlHot.setEn_thk(q652TPdiData.getThickness());
                tbDsjColdNlHot.setEn_wid(q652TPdiData.getWidth());
                tbDsjColdNlHot.setEn_len(q652TPdiData.getLength());
                tbDsjColdNlHot.setEn_wt(q652TPdiData.getWeight());
                tbDsjColdNlHot.setEn_diam_in(q652TPdiData.getDiam_inside());
                tbDsjColdNlHot.setEn_diam_out(q652TPdiData.getDiam_outside());
                tbDsjColdNlHot.setEn_thk_max(q652TPdiData.getExit_thick_max());
                tbDsjColdNlHot.setEn_thk_min(q652TPdiData.getExit_thick_min());
                tbDsjColdNlHot.setEn_wid_max(q652TPdiData.getExit_width_max());
                tbDsjColdNlHot.setEn_wid_min(q652TPdiData.getExit_width_min());
                tbDsjColdNlHot.setTrim_flag(Double.parseDouble(q652TPdiData.getTrimming_flag()));
                tbDsjColdNlHot.setTrim_wid(q652TPdiData.getTrimming_width());
                tbDsjColdNlHot.setHeat_no(q652TPdiData.getHeat_no1());
            }
            LambdaQueryWrapper<TbDsjColdNlHot> qwr = new LambdaQueryWrapper<>();
            qwr.eq(TbDsjColdNlHot::getCold_entry_id,q652TPdoData.getExtid());
            TbDsjColdNlHot tbDsjColdNlHot1 = tbDsjColdNlHotMapper.selectOne(qwr);
            if (tbDsjColdNlHot1 != null) {
                return ResultData.ResultDataFaultSelf("该卷号已存在,请查看参数是否正确!",-1);
            }
            int saveState = tbDsjColdNlHotMapper.insert(tbDsjColdNlHot);
            return ResultData.ResultDataFaultSelf("操作成功",saveState);
        }
        return ResultData.ResultDataFaultSelf("操作失败",null);
    }
}
